package com.dstz.crm.core.manager.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.encrypt.EncryptUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.manager.impl.BaseManager;
import com.dstz.crm.core.dao.CustomerFollowupDao;
import com.dstz.crm.core.manager.CustomerFollowupManager;
import com.dstz.crm.core.manager.CustomerManager;
import com.dstz.crm.core.model.Customer;
import com.dstz.crm.core.model.CustomerFollowup;
import com.dstz.crm.core.model.CustomerRelation;
import com.dstz.org.api.model.IUser;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 案例 Manager处理实现类
 * @author aschs
 * @email aschs@qq.com
 * @time 2018-08-24 18:06:04
 */
@Service("customerFollowupManager")
public class CustomerFollowupManagerImpl extends BaseManager<String, CustomerFollowup> implements CustomerFollowupManager{
	@Resource
	CustomerFollowupDao customerFollowupDao;
	@Resource
	CustomerManager customerManager;
	 

	@Override
	public CustomerFollowup get(String entityId) {
		CustomerFollowup customerFollowup =  super.get(entityId);
		if(customerFollowup!= null) {
			List<Customer> customerList=new ArrayList<>();
			Customer customer = customerManager.get(customerFollowup.getCustomerId());
			if(customer!=null) {
				customerList.add(customer);
				customerFollowup.setCustomerList(customerList);
			}
		}
		return customerFollowup;
	}


	@Override
	public void saveCustomerFollowup(CustomerFollowup customerFollowup) {
		List<Customer> customerList = customerFollowup.getCustomerList();
		if(customerList==null) {
			throw new BusinessException( "客户不可为空！");
		}
		customerFollowup.setCustomerId(customerList.get(0).getId());
		
		if(StringUtil.isEmpty(customerFollowup.getId())) {
			this.create(customerFollowup);
		}else {
			this.update(customerFollowup);
		}
		customerManager.updateFollowup(customerFollowup.getCustomerId());
	}

	@Override
	public void remove(String entityId) {
		CustomerFollowup customerFollowup = dao.get(entityId);
        dao.remove(entityId);
        if(customerFollowup!=null) {
        	customerManager.updateFollowup(customerFollowup.getCustomerId());
        }
    }
	
}
